from django_filters.rest_framework import FilterSet
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateAPIView

from .serializers import *


class ProjectFilter(FilterSet):
    class Meta:
        model = Project
        fields = {
            'status': ['exact'],
        }


class ProjectListCreateAPIView(ListCreateAPIView):
    queryset = Project.objects.order_by('date_start')
    serializer_class = ProjectSerializer
    filterset_class = ProjectFilter
    search_fields = ['code', 'name']

    def perform_create(self, serializer):
        serializer.validated_data['created_by_id'] = self.request.user.id
        serializer.validated_data['updated_by_id'] = self.request.user.id
        if self.request.user.has_perms('approval:change_project'):
            serializer.validated_data['status'] = 2
        else:
            serializer.validated_data['status'] = 1
        serializer.save()


class ProjectRetrieveUpdateAPIView(RetrieveUpdateAPIView):
    serializer_class = ProjectSerializer
    queryset = Project.objects.all()

    def perform_update(self, serializer):
        serializer.validated_data['updated_by_id'] = self.request.user.id
        serializer.save()


class ApplicationTypeListCreateAPIView(ListCreateAPIView):
    queryset = ApplicationType.objects.order_by('name')
    serializer_class = ApplicationTypeSerializer
    search_fields = ['code', 'name']


class ApplicationTypeRetrieveUpdateAPIView(RetrieveUpdateAPIView):
    serializer_class = ApplicationTypeSerializer
    queryset = ApplicationType.objects.all()


class ApplicationFilter(FilterSet):
    class Meta:
        model = Application
        fields = {
            'related_project': ['exact'],
            'related_application_type': ['exact'],
            'status': ['exact'],
        }


class ApplicationListCreateAPIView(ListCreateAPIView):
    queryset = Application.objects.order_by('-pk')
    serializer_class = ApplicationSerializer
    filterset_class = ApplicationFilter
    search_fields = ['code', 'name']

    def perform_create(self, serializer):
        serializer.validated_data['created_by_id'] = self.request.user.id
        serializer.validated_data['updated_by_id'] = self.request.user.id
        serializer.validated_data['status'] = 1
        serializer.save()

    def get_queryset(self):
        related_users_id_list = get_related_users(self.request, s='subordinates')
        queryset = self.queryset.filter(created_by_id__in=related_users_id_list)
        return queryset


class ApplicationRetrieveUpdateAPIView(RetrieveUpdateAPIView):
    serializer_class = ApplicationSerializer
    queryset = Application.objects.all()

    def perform_update(self, serializer):
        serializer.validated_data['updated_by_id'] = self.request.user.id
        serializer.save()

    def get_queryset(self):
        related_users_id_list = get_related_users(self.request, s='subordinates')
        queryset = self.queryset.filter(created_by_id__in=related_users_id_list)
        return queryset


class ApplicationAttachmentFilter(FilterSet):
    class Meta:
        model = ApplicationAttachment
        fields = {
            'related_application': ['exact'],
        }


class ApplicationAttachmentListCreateAPIView(ListCreateAPIView):
    queryset = ApplicationAttachment.objects.all()
    serializer_class = ApplicationAttachmentSerializer
    filterset_class = ApplicationAttachmentFilter

    def get_queryset(self):
        related_users_id_list = get_related_users(self.request, s='subordinates')
        queryset = self.queryset.filter(related_application__created_by__in=related_users_id_list)
        return queryset


class ApplicationAttachmentRetrieveUpdateAPIView(RetrieveUpdateAPIView):
    serializer_class = ApplicationAttachmentSerializer
    queryset = ApplicationAttachment.objects.all()

    def get_queryset(self):
        related_users_id_list = get_related_users(self.request, s='subordinates')
        queryset = self.queryset.filter(related_application__created_by__in=related_users_id_list)
        return queryset
